var mongoose = require('mongoose');
var News = require('../models/news.model');

exports.removes = function(req,res,next){
    var ids = req.body.ids;
    if(ids.length>0){
        News.remove({_id:{$in:ids}})
        .then(data=>{
            res.json({'message':'deletes ok!'})
        })
    }
}
exports.create = function(req,res,next){
    const news = new News(req.body); // req.body
    news.save(function (err,data) {
        if (err) {
          console.log(err);
        } else {
          res.json(data);
        }
      });

}

exports.update = function(req,res,next){
    const id = req.params.id;
    // 从User这个数据模型里去找ID的对象并且将它进行更新操作
    News.findByIdAndUpdate(id, {$set: req.body},{new:false})
    .then(data=>{
        // 这个data数据返回的是没有修改之前的数据
        res.json(data);
    })
}
exports.removes = function(req,res,next){

    var ids = req.body.ids;
    console.log(ids)
    if(ids.length>0){

        News.remove({_id:{$in:ids}})
        .then(data=>{
            res.json({'message':'deletes ok!'})
        })
    }
}

exports.remove = function(req,res,next){
    const id = req.params.id;
    // 从User这个数据模型里去找ID的对象并且将它进行更新操作
    News.findByIdAndRemove(id, function(err,data){
        res.json({"message":"delete ok"});
    })
}

exports.list = function(req,res,next){
   var page = (req.body.page) ? req.body.page: 1;
   var limit = (req.body.limit) ? req.body.limit: 6;
//    News.paginate({},{page:page,limit:limit},function(err,results){
//        res.json(results)
//    })
   // 查询条件
   console.log(req.body.limit);
   var queryCondition = {};
   if(req.body.name && req.body.name.trim().length>0){
       
       name = req.body.name;
       queryCondition = {
            "global": new RegExp(name,"i")
       }
   }

   News.paginate({_id:req.params.id},{page: page,limit:limit},function(err,results){
       res.json(results)
   })

}
exports.info =function(req,res,next){
    var page = (req.params.page) ? req.params.page: 1;
    console.log(page);
   var limit = (req.body.limit) ? req.body.limit: 6;
    var a={};
    if(req.params.id==0){
       a={id:0}
    }else if(req.params.id==1){
        a={id:1}
    }else if(req.params.id==2.1){
        a={ id:2.1}
    }else{
        a={id:2.2}
    }
    News.paginate(a,{page:page,limit:limit},function(err,results){
        res.json(results)
    })
}  